/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007-2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.projects.toolbox_basics_project.bean.impl;

import java.util.Set;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;

import com.genia.toolbox.basics.exception.technical.TechnicalException;
import com.genia.toolbox.basics.manager.ExceptionManager;
import com.genia.toolbox.projects.toolbox_basics_project.bean.ParameterHandler;
import com.genia.toolbox.projects.toolbox_basics_project.bean.PersistableApplicationParameter;
import com.genia.toolbox.projects.toolbox_basics_project.spring.manager.ParameterManager;

/**
 * Default implementation of {@link PersistableApplicationParameterHandler}.
 */
public abstract class AbstractDefaultParameterHandler
    implements ParameterHandler<Object>, BeanFactoryAware
{

  /**
   * the {@link ParameterManager} to use.
   */
  private ParameterManager parameterManager;

  /**
   * the {@link ExceptionManager} to use.
   */
  private ExceptionManager exceptionManager;

  /**
   * the {@link ConfigurableBeanFactory} to use to retrieve
   * {@link org.springframework.beans.TypeConverter}.
   */
  private transient ConfigurableBeanFactory beanFactory;



  /**
   * returns the {@link Set} of {@link Class} that this handler handle.
   * 
   * @return the {@link Set} of {@link Class} that this handler handle or
   *         <code>null</code> if this handler is the default one
   * @see com.genia.toolbox.projects.toolbox_basics_project.bean.PersistableApplicationParameterHandler#getHandlingClasses()
   */
  public Set<Class<?>> getHandlingClasses()
  {
    return null;
  }



  /**
   * convert a {@link String} to the correct type dependeing on the
   * {@link PersistableApplicationParameter}.
   * 
   * @param applicationParameter
   *          the {@link PersistableApplicationParameter} to do the conversion
   *          for
   * @param value
   *          the string value
   * @return the value converted
   * @throws TechnicalException
   *           if an error occured
   */
  protected Object getValue(PersistableApplicationParameter<Object> applicationParameter, String value)
      throws TechnicalException
  {
    try {
      return beanFactory.getTypeConverter().convertIfNecessary(value, applicationParameter.getParameterClass());
    }
    catch (Exception e) {
      throw getExceptionManager().convertException(e);
    }
  }



  /**
   * Callback that supplies the owning factory to a bean instance.
   * <p>
   * Invoked after the population of normal bean properties but before an
   * initialization callback such as
   * {@link org.springframework.beans.factory.InitializingBean#afterPropertiesSet()}
   * or a custom init-method.
   * 
   * @param beanFactory
   *          owning BeanFactory (never <code>null</code>). The bean can
   *          immediately call methods on the factory.
   * @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory)
   */
  public void setBeanFactory(BeanFactory beanFactory)
  {
    if (beanFactory != null && beanFactory instanceof ConfigurableBeanFactory) {
      this.beanFactory = (ConfigurableBeanFactory) beanFactory;
    }
  }



  /**
   * getter for the parameterManager property.
   * 
   * @return the parameterManager
   */
  public ParameterManager getParameterManager()
  {
    return parameterManager;
  }



  /**
   * setter for the parameterManager property.
   * 
   * @param parameterManager
   *          the parameterManager to set
   */
  public void setParameterManager(ParameterManager parameterManager)
  {
    this.parameterManager = parameterManager;
  }



  /**
   * getter for the exceptionManager property.
   * 
   * @return the exceptionManager
   */
  public ExceptionManager getExceptionManager()
  {
    return exceptionManager;
  }



  /**
   * setter for the exceptionManager property.
   * 
   * @param exceptionManager
   *          the exceptionManager to set
   */
  public void setExceptionManager(ExceptionManager exceptionManager)
  {
    this.exceptionManager = exceptionManager;
  }

}
